package tv.huan.media.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tv.huan.common.core.text.Convert;
import tv.huan.common.utils.DateUtils;
import tv.huan.common.utils.http.HttpUtils;
import tv.huan.media.domain.Media;
import tv.huan.media.domain.TencentInfo;
import tv.huan.media.mapper.MediaAuditMapper;
import tv.huan.media.domain.MediaAudit;
import tv.huan.media.mapper.MediaMapper;
import tv.huan.media.mapper.TencentInfoMapper;
import tv.huan.media.service.IMediaAuditService;
import tv.huan.system.mapper.SysDeptMapper;

/**
 * 媒资审核记录Service业务层处理
 * 
 * @author admin
 * @date 2020-04-03
 */
@Service
public class MediaAuditServiceImpl implements IMediaAuditService 
{
    @Autowired
    private MediaAuditMapper mediaAuditMapper;

    @Autowired
    private TencentInfoMapper tencentInfoMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private MediaMapper mediaMapper;

    @Value("${huan.sendAuditResult}")
    private boolean sendAuditResult;

    /**
     * 查询媒资审核记录
     * 
     * @param id 媒资审核记录ID
     * @return 媒资审核记录
     */
    @Override
    public MediaAudit selectMediaAuditById(Long id)
    {
        return mediaAuditMapper.selectMediaAuditById(id);
    }

    /**
     * 查询媒资审核记录列表
     * 
     * @param mediaAudit 媒资审核记录
     * @return 媒资审核记录
     */
    @Override
    public List<MediaAudit> selectMediaAuditList(MediaAudit mediaAudit)
    {
        return mediaAuditMapper.selectMediaAuditList(mediaAudit);
    }

    /**
     * 新增媒资审核记录
     * 
     * @param mediaAudit 媒资审核记录
     * @return 结果
     */
    @Override
    public int insertMediaAudit(MediaAudit mediaAudit)
    {
        return mediaAuditMapper.insertMediaAudit(mediaAudit);
    }

    /**
     * 修改媒资审核记录
     * 
     * @param mediaAudit 媒资审核记录
     * @return 结果
     */
    @Override
    public int updateMediaAudit(MediaAudit mediaAudit)
    {
        return mediaAuditMapper.updateMediaAudit(mediaAudit);
    }

    /**
     * 删除媒资审核记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteMediaAuditByIds(String ids)
    {
        return mediaAuditMapper.deleteMediaAuditByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除媒资审核记录信息
     * 
     * @param id 媒资审核记录ID
     * @return 结果
     */
    public int deleteMediaAuditById(Long id)
    {
        return mediaAuditMapper.deleteMediaAuditById(id);
    }
    /**
     * 审核
     *
     * @param id 视频id
     * @param auditState 审核结果
     * @param user 审核人
     * @return 结果
     */
    @Override
    @Transactional
    public int audit(Integer id, Integer auditState, String user, String auditFailedReason) throws Exception{
        Media media = mediaMapper.selectMediaById(id);
        if (media.getStatus() == 2 || media.getStatus() == -1){
            throw new Exception("存在已审核完成的媒资！");
        }
        //获取审核前状态
        MediaAudit currentAudit = mediaAuditMapper.selectMediaAuditByMediaId(id);
        //审核前轮次
        Integer auditRound = currentAudit.getAuditRound();
        //获取腾讯配置信息
        TencentInfo tencentInfo = tencentInfoMapper.selectTencentInfoById(1);
        //部门名称
        String[] deptNames = tencentInfo.getAuditDeptName().split(",");
        Long deptId;
        //一审
        if (auditRound == 0){
            if (auditState == 1){
                currentAudit.setOneAuditUser(user);
                currentAudit.setOneAuditTime(DateUtils.getNowDate());
            }else if (auditState == -1){
                currentAudit.setOneAuditUser(user);
                currentAudit.setOneAuditTime(DateUtils.getNowDate());
                currentAudit.setAuditState(-1);
                currentAudit.setAuditFailedReason(auditFailedReason);
            }
            //将数据权限改为二审部门
            deptId = this.sysDeptMapper.selectDeptByName(deptNames[1]).getDeptId();
            currentAudit.setDeptId(deptId);
        }
        //二审
        if (auditRound == 1){
            if (auditState == 1){
                currentAudit.setTowAuditUser(user);
                currentAudit.setTowAuditTime(DateUtils.getNowDate());
            }else if (auditState == -1){
                currentAudit.setTowAuditUser(user);
                currentAudit.setTowAuditTime(DateUtils.getNowDate());
                currentAudit.setAuditState(-1);
                currentAudit.setAuditFailedReason(auditFailedReason);
            }
            //将数据权限改为三审部门
            deptId = this.sysDeptMapper.selectDeptByName(deptNames[2]).getDeptId();
            currentAudit.setDeptId(deptId);
        }
        //三审
        if (auditRound == 2){
            if (auditState == 1){
                currentAudit.setThreeAuditUser(user);
                currentAudit.setThreeAuditTime(DateUtils.getNowDate());
                currentAudit.setAuditState(1);
            }else if (auditState == -1){
                currentAudit.setThreeAuditUser(user);
                currentAudit.setThreeAuditTime(DateUtils.getNowDate());
                currentAudit.setAuditState(-1);
                currentAudit.setAuditFailedReason(auditFailedReason);
            }
        }
        currentAudit.setAuditRound(auditRound + 1);

        //推送审核结果&&更新媒资状态
        String param = getAuditResultJson(media.getCid(),media.getVid(),auditState,auditFailedReason);
        if (auditState == -1){
            //更新媒资列表状态 被驳回
            media.setStatus(-1);
            if (sendAuditResult){
                //推送审核结果
                HttpUtils.postWithJson(tencentInfo.getPushResultAddress(),param);
            }
        }else {
            if (auditRound < 2){
                //更新媒资列表状态 审核中
                media.setStatus(1);
            }
            //三审成功，审核前的轮次是2
            if (auditRound == 2){
                //更新媒资列表状态 审核成功
                media.setStatus(2);
                if (sendAuditResult){
                    //推送审核结果
                    HttpUtils.postWithJson(tencentInfo.getPushResultAddress(),param);
                }
            }
        }
        mediaMapper.updateMedia(media);
        return mediaAuditMapper.updateMediaAudit(currentAudit);
    }

    private String getAuditResultJson(String cid, String vid, Integer auditState, String auditFailedReason){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("cid",cid);
        jsonObject.put("vid",vid);
        jsonObject.put("auditState",auditState);
        jsonObject.put("auditFailedReason",auditFailedReason);
        return jsonObject.toString();
    }
}
